package com.coollang.smartbra.fragment;

import android.support.v4.content.ContextCompat;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.coollang.smartbra.R;
import com.coollang.smartbra.adapter.FragmentSportAdapter;
import com.coollang.smartbra.base.BaseFragment;
import com.coollang.smartbra.base.MyApplication;
import com.coollang.smartbra.beans.FragmentSportItem;
import com.coollang.smartbra.constant.ResourceList;
import com.coollang.smartbra.utils.ActivitySwitcher;
import com.coollang.smartbra.utils.ToastUtils;
import com.coollang.smartbra.utils.TranslucentSystemBarUtils;
import com.coollang.smartbra.utils.UIUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @author Timmy
 *         运动页面
 */
public class SportFragment extends BaseFragment implements BaseQuickAdapter.OnItemClickListener {
    @BindView(R.id.tv_calendar_realtime)
    TextView tvCalendarRealtime;
    @BindView(R.id.tv_title_realtime)
    TextView tvTitleRealtime;
    @BindView(R.id.view)
    View mView;
    @BindView(R.id.status_bar)
    View statusbar;
    @BindView(R.id.fragment_sport_ry)
    RecyclerView mRecyclerView;
    private FragmentSportAdapter mAdapter;

    @Override
    public View inflateView(LayoutInflater inflater) {
        View view = inflater.inflate(R.layout.fragment_sport, null, false);
        ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void initView(View view) {
        initViews();
        TranslucentSystemBarUtils.showChenJinZhuangTaiLan(statusbar);
    }

    private void initViews() {
        tvCalendarRealtime.setVisibility(View.GONE);
        tvTitleRealtime.setText(getString(R.string.sport));
        tvTitleRealtime.setTextColor(ContextCompat.getColor(getContext(), R.color.black_666));
        mView.setVisibility(View.VISIBLE);

        mRecyclerView.setLayoutManager(new GridLayoutManager(getContext(), 2));
        mAdapter = new FragmentSportAdapter(null);
        mRecyclerView.setAdapter(mAdapter);

        mAdapter.setOnItemClickListener(this);

    }

    @Override
    public void initData() {
        String[] actionNames = ResourceList.ACTION_NAME_LIST;
        int[] itemImages = ResourceList.FRAGMENT_ITEM_IMAGES;

        List<FragmentSportItem> itemList = new ArrayList<>();
        for (int i = 0; i < actionNames.length; i++) {
            FragmentSportItem item = new FragmentSportItem();
            item.setSportName(actionNames[i]);
            item.setImageId(itemImages[i]);
            itemList.add(item);
        }
        mAdapter.addData(itemList);
    }

    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
        if (position == adapter.getItemCount() - 1) {
            goMapAct();
        } else {
            goAction(position);
        }
    }

    private void goMapAct() {
        if (MyApplication.getApplication().isConnected) {
            ActivitySwitcher.goMapAct1(getActivity());
        } else {
            ToastUtils.showToast(UIUtils.getString(R.string.ble_disconnect));
        }
    }

    private void goAction(int position) {
        if (MyApplication.getApplication().isConnected) {
            ActivitySwitcher.goToNewAction(getActivity(), position);
        } else {
            ToastUtils.showToast(UIUtils.getString(R.string.ble_disconnect));
        }
    }
}
